#!/usr/bin/perl

# Stop us from shooting outselves in the foot
use strict;
use warnings;
use FindBin qw($Bin);
use lib "$Bin";

# We need to interact with MediaWiki
use CMS::MediaWiki;
use Text::Template;    # We use a template, makes life easier

use MDBUtils;
use MDBAWS;
use MDBWP;

# Configuration
my $config = MDBUtils::get_config();
my $queue  = MDBAWS::sqs_get_queue("mediawiki");

###############################
# Setup MediaWiki environment #
###############################

my $mw = CMS::MediaWiki->new(
    host => $config->{'mediawiki'}->{'host'},
    path => $config->{'mediawiki'}->{'path'}
);

if (
    $mw->login(
        user => $config->{'mediawiki'}->{'username'},
        pass => $config->{'mediawiki'}->{'password'}
    )
  )
{
    print STDERR "Could not login\n";
    exit;
}
else {

    while (1) {
        my $msg = MDBAWS::sqs_get_message($queue);

        if ( !defined($msg) ) {
            print "DEBUG: No tickets in the queue, quiting...\n";
            exit(0);
        }
        else {
            print "DEBUG: Picked up SQS ticket: " . $msg->MessageBody() . "\n";

            my $metadata = MDBAWS::sdb_get_metadata( $msg->MessageBody() );

            if ( defined( $metadata->{'sha256'} ) ) {

                my $vt_flagged = 0;
                my $vt_total   = 0;
                my $table      = "\n{| border=\"1\" cellpadding=\"2\" \n";
                $table = $table . "|-\n";
                $table = $table . "| '''AV Engine''' || '''Result'''\n";

                while ( my ( $engine, $result ) = each(%$metadata) ) {
                    if ( $engine =~ m/vt_/ ) {
                        $engine =~ s/vt_//;
                        $vt_total++;

                        if ( $result ne '-' ) {
                            $vt_flagged++;
                        }

                        $table = $table . "|-\n";
                        $table = $table . "| $engine || $result\n";
                    }
                }
                $table = $table . "|}\n";

                if ( $vt_total > 0 ) {
                    $metadata->{'vt_scan_results'} = $table;
                    $metadata->{'vt_flagged'}      = $vt_flagged;
                    $metadata->{'vt_total'}        = $vt_total;
                    $metadata->{'vt_percent'} =
                      sprintf( "%.2f", ( $vt_flagged / $vt_total ) * 100 );
                }

                my $template =
                  Text::Template->new( SOURCE => $Bin . '/mediawiki_report.tmpl' )
                  or die "Couldn't construct template: $Text::Template::ERROR";

                my $wikitext = $template->fill_in( HASH => $metadata );

                if ( defined $wikitext ) {
                    #######################
                    # Upload data to Wiki #
                    #######################

                    $mw->editPage(
                        title   => $metadata->{'sha256'},
                        section => '',
                        text    => $wikitext,
                        summary => "Updated article"
                    );
                }
                else {
                    die "Couldn't fill in template: $Text::Template::ERROR";
                }
            }

            # Create SQS Ticket(s)
            MDBAWS::sqs_put_queue( "mediawiki", $msg->MessageBody() );

            print "DEBUG: Deleting SQS ticket: " . $msg->MessageBody() . "\n";
            $queue->DeleteMessage( $msg->ReceiptHandle() );
        }
    }
}
